package it.unibo.convoycruisecontrol.chiefboatsubsystem.test.unit;

import static org.junit.Assert.*;
import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocation;
import it.unibo.convoycruisecontrol.boatsubsystem.test.mock.MockLocationDevice;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.MapManager;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.test.mock.MockMapManager;
import it.unibo.location.ILocation;
import it.unibo.util.information.PrologInformationConverter;

import org.junit.BeforeClass;
import org.junit.Test;

public class MapManagerTestCase {

	static MockMapManager mapManager;
	static int initRefreshTime = 500;
	static int boatsNumber = 2;
	String message1 = "positions([boatInfo([],speed(45.0),longitude(34.0),latitude(65.0),sequenceNumber(0)),"+
						"boatInfo([],speed(56.0),longitude(78.0),latitude(23.0),sequenceNumber(1))])";
	
	
	String message2 = "positions([boatInfo([],speed(89.0),longitude(12.0),latitude(54.0),sequenceNumber(0)),"+
						"boatInfo([],speed(79.0),longitude(67.0),latitude(34.0),sequenceNumber(1))])";

	@BeforeClass
	public static void beforeClass()
	{
		try
		{
			mapManager = new MockMapManager(new MockLocationDevice(), initRefreshTime, boatsNumber, new PrologInformationConverter());
		}
		catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void testCreateMessage() 
	{
		mapManager.setSpeeds(new float[]{45.0f, 56.0f});
		mapManager.setLocations(new ILocation[]{new MockLocation(65.0, 34.0, 0.0), new MockLocation(23.0, 78.0, 0.0)});
		assertEquals(message1, mapManager.createMessage());
		
		mapManager.setSpeeds(new float[]{89.0f, 79.0f});
		mapManager.setLocations(new ILocation[]{new MockLocation(54.0, 12.0, 0.0), new MockLocation(34.0, 67.0, 0.0)});
		assertEquals(message2, mapManager.createMessage());
	}


	// Controllo che il refresh time sia impostato correttamente e che lanci eccezione in caso di tempo negativo
	@Test(expected=IllegalArgumentException.class)
	public void testRefreshTime()
	{
		mapManager.setRefreshTime(initRefreshTime);
		assertEquals(initRefreshTime, mapManager.getRefreshTime());
		mapManager.setRefreshTime(5000);
		assertEquals(5000, mapManager.getRefreshTime());

		mapManager.setRefreshTime(-5);
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void testCreation() throws Exception
	{
		new MapManager(null, -8, -9, null);
	}

}
